Imports DAL
Imports MyClasses
Imports System.Configuration

Public Class frmRolePrivilige
    Dim _intRoleID As Integer
    Public IsPrivDataSaved As Boolean

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub frmRolePrivilige_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Roles And Privileges")
        e.Cancel = True
    End Sub

    Private Sub frmRolePrivilige_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Roles And Privileges")
        End If
    End Sub

    Private Sub frmRolePrivilige_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            _intRoleID = BCommon.GetSeqNum("Role")
            Call BCommon.SetFormStandards(Me)
            Call LoadListBox()
            Call ClearData()
            txtRole.Focus()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmRolePrivilige_Load")
        End Try
    End Sub

    Private Sub LoadListBox()
        Dim dtLst As New DataTable
        Dim count As Integer
        Try
            dtLst = BRole.GetPrivileges()
            If dtLst.Rows.Count > 0 Then
                For count = 0 To dtLst.Rows.Count - 1
                    lstPrivilege.Items.Add(dtLst.Rows(count).Item(1), False)
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "LoadListBox")
        End Try
    End Sub

    Public Sub SaveData()

        Dim objRole As New CRole
        Try
            Dim blnResult As Boolean
            If ValidateData() = False Then Exit Sub
            objRole = SetRole()
            If btnSave.Text = "&Save" Then
                blnResult = BRole.InsertRoles(SetPriv(), "S", objRole)
                If blnResult = True Then BCommon.ShowMsgError(3) : Call ClearData() Else BCommon.ShowMsgError(4)
            Else
                blnResult = BRole.InsertRoles(SetPriv(), "U", objRole)
                If blnResult = True Then
                    BCommon.ShowMsgError(5)
                    IsPrivDataSaved = True
                    btnSave.Text = "&Save"
                    Call ClearData()
                Else
                    BCommon.ShowMsgError(4)
                End If
            End If
            txtRole.Focus()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
        End Try
    End Sub

    Public Function ValidateData() As Boolean
        Dim count As Integer
        Dim dtRoles As New DataTable

        If Trim(txtRole.Text) = "" Then
            BCommon.ShowMsgError(10410)
            txtRole.Focus()
            Return False
        End If

        If btnSave.Text = "&Save" Then
            dtRoles = BRole.GetRoles()
            If dtRoles.Rows.Count > 0 Then
                For count = 0 To dtRoles.Rows.Count - 1
                    If UCase(Trim(txtRole.Text)) = UCase(dtRoles.Rows(count).Item("Name")) Then
                        BCommon.ShowMsgError(10411)
                        txtRole.Focus()
                        Return False
                    End If
                Next
            End If
        End If

        If lstPrivilege.CheckedItems.Count < 1 Then
            BCommon.ShowMsgError(10412)
            lstPrivilege.Focus()
            Return False
        End If

        Return True
    End Function

    Public Sub ViewRoles(ByVal RoleID As Integer)
        Dim count As Integer
        Dim dtRoles As New DataTable
        Try
            dtRoles = BRole.GetSearchedItems(RoleID)
            If dtRoles.Rows.Count > 0 Then
                txtRole.Text = dtRoles.Rows(0).Item("Name")
                txtRole.Tag = txtRole.Text
                For count = 0 To dtRoles.Rows.Count - 1
                    lstPrivilege.SetItemCheckState(dtRoles.Rows(count).Item("PRID") - 1, CheckState.Checked)
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewRoles")
        End Try
    End Sub

    Private Sub ClearData()
        Dim count As Integer
        Try
            txtRole.Text = ""
            txtRole.Tag = txtRole.Text
            btnDelete.Enabled = False
            For count = 0 To lstPrivilege.Items.Count - 1
                lstPrivilege.SetItemCheckState(count, CheckState.Unchecked)
            Next
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub

    Private Function isUpdate() As Boolean
        Try
            If Trim(txtRole.Text) <> txtRole.Tag Then
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
        End Try
    End Function

    Private Function SetRole() As CRole
        Try
            Dim role As New CRole
            role.RoleName = txtRole.Text
            role.RoleID = _intRoleID
            Return role
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetRoleID")
            Return Nothing
        End Try
    End Function

    Private Function SetPriv()
        Dim intPriv(lstPrivilege.CheckedItems.Count - 1) As Integer
        'Dim count As Integer
        Try
            For count As Integer = 0 To lstPrivilege.CheckedItems.Count - 1
                intPriv(count) = BRole.GetPrivileges(lstPrivilege.CheckedItems.Item(count).ToString)
            Next
            Return intPriv
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetPriv")
            Return Nothing
        End Try
    End Function

    Private Sub txtRole_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRole.KeyDown
        If e.Alt Then Exit Sub
        If e.KeyCode = Keys.Enter Then lstPrivilege.Focus()
        If BCommon.ValidateText(e.KeyCode, "ALFNUM") = False Or e.KeyCode = 191 Then    'For restricting ForwardSlash(keyCode-191)
            e.SuppressKeyPress = True
        End If
        If e.Shift = True Then
            If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Or e.KeyCode = 190 Or e.KeyCode = 222 Then
                e.SuppressKeyPress = True
            End If
        End If
        If e.KeyCode = 222 Then e.SuppressKeyPress = False 'Single Quotes
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Call SaveData()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtRole.Focus()
        btnSave.Text = "&Save"
        Call ClearData()
    End Sub

    Private Sub btnSrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click
        Dim form As New frmMasterBrowse
        Try
            form.strSearch = "Role"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            form.ShowDialog()
            _intRoleID = form.strRecordNo
            Call ClearData()
            If _intRoleID <> 0 Then
                Call ViewRoles(_intRoleID)
                btnSave.Text = "&Update"
                If ConfigurationManager.AppSettings("FormMode") = "R" Then
                    btnDelete.Enabled = False
                Else
                    btnDelete.Enabled = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSrch_Click")
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmNewUser_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateData() Then
                                Call SaveData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim dtUserRole As New DataTable
        Dim count As Integer
        Dim blnGotResult As Boolean
        Dim blnResult As Boolean
        Try
            dtUserRole = BRole.GetUserRole()
            If dtUserRole.Rows.Count > 0 Then
                For count = 0 To dtUserRole.Rows.Count - 1
                    If dtUserRole.Rows(count).Item("RoleID") = _intRoleID Then
                        blnGotResult = True
                    End If
                Next
            End If
            If blnGotResult = True Then
                BCommon.ShowMsgError(10413)
            Else
                blnResult = BRole.DeleteAll(_intRoleID)
                If blnResult = True Then BCommon.ShowMsgError(10414) : btnSave.Text = "&Save" : Call ClearData() Else MessageBox.Show("Role Not Deleted.")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
        End Try
    End Sub
End Class